package com.kaoyaya.delivery.data;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.google.gson.reflect.TypeToken;
import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.EBean;
import com.googlecode.androidannotations.annotations.RootContext;
import com.googlecode.androidannotations.annotations.UiThread;
import com.googlecode.androidannotations.annotations.sharedpreferences.Pref;
import com.kaoyaya.delivery.data.entity.Client;
import com.kaoyaya.delivery.data.entity.Combo;
import com.kaoyaya.delivery.data.entity.CostRecord;
import com.kaoyaya.delivery.data.entity.DeliveryRecord;
import com.kaoyaya.delivery.data.entity.OrderItem;
import com.kaoyaya.delivery.data.entity.Product;
import com.kaoyaya.delivery.data.entity.Result;
import com.kaoyaya.delivery.data.entity.UserInfo;
import com.kaoyaya.util.HttpUtil;
import com.kaoyaya.util.JsonUtil;
import com.kaoyaya.util.StringUtil;

@EBean
public class DataHelper {
	
	@RootContext Context context;
	Handler callbackHandler;
	@Pref InfoFile_ infoFile;
	String url = Constants.SERVER_URL;
	
	/**
	 * 用户登录
	 * @author Goven
	 * @date 2013-5-31
	 */
	public void login(int flag, String username, String password) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("username", username);
		params.put("password", password);
		doInBackground(flag, url + "login.php", params, UserInfo.class);
	}
	
	/**
	 * 获取用户信息
	 * @param flag
	 * @author Goven
	 * @date 2013-6-7
	 */
	public void getUserInfo(int flag) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("username", infoFile.username().get());
		doInBackground(flag, url + "getUserInfo.php", params, Result.class);
	}
	
	/**
	 * 获取用户点数
	 * @author Goven
	 * @date 2013-6-2
	 */
	public void getUserAccount(int flag) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("partner", infoFile.userId().get());
		doInBackground(flag, url + "getUserAccount.php", params, Double.class);
	}
	
	/**
	 * 获取产品列表
	 * @param flag 数据请求标识
	 * @author Goven
	 * @date 2013-5-26
	 */
	public void getProducts(int flag, int pageIndex, String area, String name, String category, String subject, String lowest, String highest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("pageIndex", pageIndex);
		params.put("pageSize", Constants.PAGE_PRODUCT_SIZE);
		if (StringUtil.isNotTrimBlank(area)) params.put("area", area);
		if (StringUtil.isNotTrimBlank(name)) params.put("name", name);
		if (StringUtil.isNotTrimBlank(category)) params.put("category", category);
		if (StringUtil.isNotTrimBlank(subject)) params.put("subject", subject);
		if (StringUtil.isNotTrimBlank(lowest)) params.put("lowest", lowest);
		if (StringUtil.isNotTrimBlank(highest)) params.put("highest", highest);
		Type type = new TypeToken<List<Product>>() {}.getType();
		doInBackground(flag, url + "getProductList.php", params, type);
	}
	
	/**
	 * 获取地区列表
	 * @author Goven
	 * @date 2013-6-2
	 */
	public void getAreas(int flag) {
		doInBackground(flag, url + "getAreaList.php", null, String[].class);
	}
	
	/**
	 * 获取套餐列表
	 * @author Goven
	 * @date 2013-5-26
	 */
	public void getCombos(int flag, int pageIndex, String area, String name, String lowest, String highest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("pageIndex", pageIndex);
		params.put("pageSize", Constants.PAGE_COMBO_SIZE);
		if (StringUtil.isNotTrimBlank(area)) params.put("area", area);
		if (StringUtil.isNotTrimBlank(name)) params.put("name", name);
		if (StringUtil.isNotTrimBlank(lowest)) params.put("lowest", lowest);
		if (StringUtil.isNotTrimBlank(highest)) params.put("highest", highest);
		Type type = new TypeToken<List<Combo>>() {}.getType();
		doInBackground(flag, url + "getComboList.php", params, type);
	}
	
	/**
	 * 用户下单
	 * @author Goven
	 * @date 2013-6-4
	 */
	public void deliveryOrder(int flag, int count, Client client, List<OrderItem> order) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("partner", infoFile.userId().get());
		params.put("count", count);
		params.put("client", JsonUtil.toJson(client));
		params.put("order", JsonUtil.toJson(order));
		doInBackground(flag, url + "deliveryOrder.php", params, Result.class);
	}
	
	/**
	 * 获取发货记录
	 * @author Goven
	 * @date 2013-5-26
	 */
	public void getDeliveryRecords(int flag, int pageIndex) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("pageIndex", pageIndex);
		params.put("pageSize", Constants.PAGE_DEFAULT_SIZE);
		params.put("partner", infoFile.userId().get());
		Type type = new TypeToken<List<DeliveryRecord>>() {}.getType();
		doInBackground(flag, url + "getDeliveryRecordList.php", params, type);
	}
	
	public void getDeliveryRecordOrder(int flag, Long orderNo) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderNo", orderNo);
		Type type = new TypeToken<List<OrderItem>>() {}.getType();
		doInBackground(flag, url + "getDeliveryRecordOrder.php", params, type);
	}
	
	/**
	 * 重新发货
	 * @author Goven
	 * @date 2013-6-4
	 */
	public void reSendMail(int flag, Long orderNo) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderNo", orderNo);
		doInBackground(flag, url + "reSendMail.php", params, Result.class);
	}
	
	/**
	 * 退款
	 * @author Goven
	 * @date 2013-6-4
	 */
	public void refund(int flag, Long orderNo) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderNo", orderNo);
		doInBackground(flag, url + "refund.php", params, Result.class);
	}
	
	/**
	 * 获取消费记录
	 * @author Goven
	 * @date 2013-5-26
	 */
	public void getCostRecords(int flag, int pageIndex) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("pageIndex", pageIndex);
		params.put("pageSize", Constants.PAGE_DEFAULT_SIZE);
		params.put("partner", infoFile.userId().get());
		Type type = new TypeToken<List<CostRecord>>() {}.getType();
		doInBackground(flag, url + "getCostRecordList.php", params, type);
	}
	
	/**
	 * 用户意见反馈
	 * @param content 反馈内容
	 * @author Goven
	 * @date 2013-6-17
	 */
	public void feekback(int flag, String content) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", infoFile.userId().get());
		params.put("content", content);
		doInBackground(flag, url + "feekback.php", params, Result.class);
	}
	
	/**
	 * 普通下载的异步方法
	 * @author Goven
	 * @date 2013-6-6
	 */
	@Background
	public void downLoad(int flag, String url, String filePath) {
		callback(flag, HttpUtil.downLoad(url, filePath));
	}
	
	/**
	 * 读取单个字符串
	 * @author Goven
	 * @date 2013-6-6
	 */
	@Background
	public void readSingleStr(int flag, String url) {
		callback(flag, HttpUtil.readStr(url));
	}
	
	@Background
	public void getSingleStr(int flag, String url) {
		callback(flag, HttpUtil.doPost(url, null));
	}
	
	/**
	 * 后台任务
	 */
	@Background
	void doInBackground(int flag, String url, Map<String, Object> params, Type type) {
		callback(flag, JsonUtil.json2Any(HttpUtil.doPost(url, params), type));
	}
	
	@Background
	void doInBackground(int flag, String url, Map<String, Object> params, Class<?> clazz) {
		callback(flag, JsonUtil.json2Obj(HttpUtil.doPost(url, params), clazz));
	}
	
	@UiThread
	void callback(int flag, Object result) {
		if (callbackHandler != null) {
			Message msg = Message.obtain();
			msg.what = flag;
			msg.obj = result;
			callbackHandler.sendMessage(msg);
		}
	}
	
	/**
	 * 设置回调Hanlder，用来通知调用者
	 * @param callbackHandler 回调Hanlder
	 * @author Goven
	 * @date 2013-5-25
	 */
	public void setCallbackHandler(Handler callbackHandler) {
		this.callbackHandler = callbackHandler;
	}
	
}
